feat: add source-specific typed request input#10216
Conversation
b7de579 to
7c31ebe
Compare
|
The current PHPStan failure appears unrelated to this PR. It is in |
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
b21666b to
b712df7
Compare
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
I was about to approve this, but while reading the updated user guide I realized we already have Maybe this is a sign that the long-term API should be a small source-selector object instead of more methods directly on namespace CodeIgniter\Input;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
final class RequestInput
{
public function __construct(
private readonly IncomingRequest $request,
private readonly InputDataFactory $factory,
) {
}
public function get(): InputData
{
$data = $this->request->getGet();
return $this->factory->create(is_array($data) ? $data : []);
}
public function post(): InputData
{
$data = $this->request->getPost();
return $this->factory->create(is_array($data) ? $data : []);
}
public function json(): InputData
{
$data = $this->request->getJSON(true) ?? [];
if (! is_array($data)) {
throw HTTPException::forUnsupportedJSONFormat();
}
return $this->factory->create($data);
}
public function raw(): InputData
{
return $this->factory->create($this->request->getRawInput());
}
}Then in private ?RequestInput $input = null;
public function input(): RequestInput
{
return $this->input ??= new RequestInput($this, service('inputdatafactory'));
}Usage: $page = $request->input()->get()->integer('page', 1);
$name = $request->input()->json()->string('name');
$title = $request->input()->raw()->string('title');This would avoid overloading the existing |
|
Thanks @michalsn, I see what you mean. The I'm happy to reshape it around |
|
Yes, you're right, Maybe @paulbalandan can take a look at this proposal, so we can spot some weak points. |
Description
This PR proposes typed accessors for common request input sources on
IncomingRequest:It builds on
CodeIgniter\Input\InputDataand gives applications a clearer way to read request values with typed fallback helpers, without mixing GET, POST, and JSON data together.The new methods keep the source explicit:
getGetInput()reads query-string / GET parameters.getPostInput()reads POST body parameters.getJSONInput()reads JSON request body parameters.These helpers do not validate input and do not replace Validation or FormRequest. They are only for typed access to raw request data. If the data must satisfy application rules before use, Validation or FormRequest should still be used.
Tests cover GET, POST, and JSON input access, typed fallback behavior, source isolation, scalar JSON rejection, and invalid JSON behavior.
Checklist: